import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:geolocator/geolocator.dart';
import '../../utils/index.dart';
import '../../utils/gps/gps_util.dart';
import '../../utils/tool/tips_util.dart';


class GpsDemo extends StatefulWidget {
  const GpsDemo({super.key, this.params});
  final dynamic params;
  @override
  State<GpsDemo> createState() => _GpsDemoState();
}

class _GpsDemoState extends State<GpsDemo> {

  double longitude = 0;
  double latitude = 0;
  int count = 0;

  @override
  void initState() {
    super.initState();
    LogUtil.d(widget.params);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('GPS测试页面'),
      ),
      body: ListView(
        children: List.generate(1, (index) {
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  '次数：$count GPS 经度：$longitude,纬度：$latitude',
                  style: const TextStyle(fontSize: 18),
                ),
                _button("点击获取坐标",   onPressed: () {
                  Future<Position> fp = GpsUtil.determinePosition();
                    fp.then((p) {
                      LogUtil.d('次数：$count， 获取到的GPS：${p.longitude} : ${p.latitude}');
                      setState(() {
                        longitude = p.longitude;
                        latitude= p.latitude;
                        count++;
                      });
                    }).catchError((msg){
                      Tips.info(msg);
                    });

                },)
              ],
            ),
          );
        }),
      ), //
    );
  }

  Widget _button(String text, {VoidCallback? onPressed}) {
    return Container(
      margin: const EdgeInsets.only(top: 10),
      child: ElevatedButton(
        onPressed: onPressed,
        child: Text(
          text,
          style: TextStyle(fontSize: 22.sp),
        ),
      ),
    );
  }

}
